class PermissionController < ApplicationController
  layout :check_layout,:except=>[:add_permission]
  before_filter :check_authentication

  def check_layout
    if params[:project_id]
      "project"
    else
      "company"
    end
  end
  def index
  # for nave link looks people
  #@controller_name = 'people'
  @roles = @cur_user.company.roles
  @company = @cur_user.company
  end
  def inherit
    project_id = params[:project_id]
    label      = params[:label]
    company_id = @cur_user.company.id
    if project_id.to_i > 0
       npi = NoPermissionInherit.find(:first, :conditions=>["project_id = ? and identifier = ?",project_id,label])
       if npi
         npi.destroy
       else
         npi = NoPermissionInherit.new
         npi.project_id = project_id
         npi.identifier = label
         npi.save
       end
    else
       npi = NoPermissionInherit.find(:first, :conditions=>["company_id = ? and identifier = ?",company_id,label])
       if npi
         npi.destroy
       else
         npi = NoPermissionInherit.new
         npi.company_id = company_id
         npi.identifier = label
         npi.save
       end
    end
    render :update do |page|
      page.replace_html 'inherit_' + label,:partial=>"/permission/per_controll_item_inherit",:locals=>{:label=>label}
      page.visual_effect :highlight,'inherit_' + label,:duration=>10
      page.visual_effect :highlight,'div_' + label,:duration=>10
    end
  end
  def per_action
    container_id = params[:container_id]
    render :update do |page|
      page.hide('indicator_' + container_id)
      page.replace_html container_id + "_action",:partial=>'per_options',:locals=>{:container_id=>container_id}
    end
  end

  def create_from_sidebar
    @role = Role.new
    @role.identifier = params[:permission][:name]
    @role.company_id = @cur_user.company.id
    @role.save
    respond_to do |format|
        format.html { 
	  render :update do |page|
	    page.replace_html 'cat_side_show',:partial=>'role_list',:locals=>{:project=>params[:project_id],:label=>params[:permission][:label]}
            if params[:project_id]
              @us = @cur_user.project_users(params[:project_id]) - [@project.company.account_owner]
              @us_no = @cur_user.company.users - @cur_user.company.project_users(params[:project_id]) - [@project.company.account_owner]
            else
              @us = @cur_user.company.users - [@cur_user.company.account_owner]
              @us_no = @cur_user.company.removed_users
            end
	    #page.replace_html 'user_list', :partial=>'/people/user_list',:locals=>{:project_id=>params[:project_id]}
	    page.visual_effect :highlight,'cat_item_' + @role.id.to_s
	    page.hide 'rename'
	    page.show 'new_cat'
	  end
	}
    end
  end

  def add_permission
    container_id = params[:container_id]
    role_id = params[:role_id].split("_")

    #get permission
    sp = StaticPermission.find_by_identifier(container_id)
    unless sp
     sp = StaticPermission.new(:identifier=>container_id)
     sp.save
    end
  
    #from a role
    if role_id.first == "r"
      role = Role.find(role_id.last)
      #add permition to role
      unless role.has_static_permission?(container_id)
        role.static_permissions << sp
	role.reload
      end
    elsif role_id.first == "u" 
      user = User.find(role_id.last)
      role = Role.find_by_identifier("custom.#{user.id}.role")
      if role 
        #custom role exist add permition to user
        if user.has_role?("custom.#{user.id}.role")
           role.static_permissions << sp
	   role.reload
        else
           user.roles << role
           role.static_permissions << sp
        end
      else
	   #create the user custom role
	   role = Role.new(:identifier=>"custom.#{user.id}.role")
           role.static_permissions << sp
	   role.save
	   user.roles << role
           user.reload
      end
    end

   render :update do |page|
     page.visual_effect :highlight,container_id,:duration=>5
     page.hide('indicator_' + container_id)
     page.replace_html container_id,:partial=>'per_item',:locals=>{:container_id=>container_id}
   end
  end
  def remove_permission
    container_id = params[:container_id]
    role_id = params[:role_id].split("_")
    if role_id.first == "r"
      role = Role.find(role_id.last)
    else
      user = User.find(role_id.last)
    end
    #add prmissions to roles
    sp = StaticPermission.find_by_identifier(container_id)
    if role
      role.static_permissions.delete(sp)
    else
      Role.find_by_identifier("custom.#{user.id}.role").static_permissions.delete(sp)
    end
  render :update do |page|
    page.visual_effect :highlight,container_id,:duration=>15
    page.hide('indicator_' + container_id)
    page.replace_html container_id,:partial=>'per_item',:locals=>{:container_id=>container_id}
  end
  end

  # role management
  def new_role
    @role = Role.new
    render :partial=>'new_item'
  end
  def cancel_new
    render :update do |page|
      page.replace_html 'new_cat', :partial=>'new_item_label',:locals=>{:project=>nil}
     end 
  end
  def edit
    @role = Role.find(params[:id])
    render :partial=>'edit_item'
  end
  def update
    @role= Role.find(params[:role][:id])

    respond_to do |format|
      if @role.update_attributes(params[:role])
        flash[:notice] = 'Role was successfully updated.'
        format.html { 
	 render :update do |page|
	 page.replace_html 'cat_item_' + @role.id.to_s, :partial=>'show_item',:locals=>{:cat=>@role}
	 end
	}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @role.errors.to_xml }
      end
    end
  end

  def cancel_edit
    @role = Role.find(params[:id])

	 render :update do |page|
	 page.replace_html 'cat_item_' + @role.id.to_s, :partial=>'show_item',:locals=>{:cat=>@role}
	end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @role = Role.find(params[:id])
    @role.destroy
    respond_to do |format|
      format.html { 
        render :update do |page|
	 page.replace_html 'cat_side_show',:partial=>'role_list',:locals=>{:label=>'ss'}
	 page.hide 'rename'
	 page.show 'new_cat'
	 end
      }
      format.xml  { head :ok }
    end
  end
end
